/*
 * Copyright 2013-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xx.jenkinsdemo.controller;

import com.xx.jenkinsdemo.vo.MessageBean;
import com.xx.jenkinsdemo.vo.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
 */
@Controller
@Slf4j
public class RabbitMqController {

    @Resource
    private RabbitTemplate rabbitTemplate;

    // http://127.0.0.1:8081/deadLetter?delayTime=30
    @RequestMapping("/deadLetter")
    @ResponseBody
    public String deadLetter(Long delayTime) {
        User user = new User();
        user.setAge(10);
        user.setName("张三");
        log.info("开始发送MQ消息__时间: {}", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        MessagePostProcessor messagePostProcessor = setExpiration(delayTime);
        if (messagePostProcessor == null) {
            rabbitTemplate.convertAndSend("baseExchange", "baseKey", MessageBean.create(user));
        } else {
            rabbitTemplate.convertAndSend("baseExchange", "baseKey", MessageBean.create(user), messagePostProcessor);
        }
        return "发送死信消息任务成功,延迟时间毫秒值: " + delayTime;
    }

    private MessagePostProcessor setExpiration(Long ttl) {
        return ttl == null ? null : message -> {
            MessageProperties messageProperties = message.getMessageProperties();
            messageProperties.setExpiration(String.valueOf(ttl)); //必须是数字字符串.不能是空
            return message;
        };
    }

    // http://127.0.0.1:8081/delayedLetter?delayTime=3000
    @RequestMapping("/delayedLetter")
    @ResponseBody
    public String delayedLetter(Integer delayTime) {
        User user = new User();
        user.setAge(10);
        user.setName("张三");
        log.info("开始发送MQ延迟消息__时间: {}", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        MessagePostProcessor messagePostProcessor = setDelay(delayTime);
        if (messagePostProcessor == null) {
            rabbitTemplate.convertAndSend("delayedExchange", "delayedKey", MessageBean.create(user));
        } else {
            rabbitTemplate.convertAndSend("delayedExchange", "delayedKey", MessageBean.create(user), messagePostProcessor);
        }
        return "发送延迟消息任务成功,延迟时间毫秒值: " + delayTime;
    }

    private MessagePostProcessor setDelay(Integer delayTime) {
        return delayTime == null ? null : message -> {
            MessageProperties messageProperties = message.getMessageProperties();
            messageProperties.setDelay(delayTime);  //允许赋值null
            return message;
        };
    }
}
